/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

/**
 * @Title: TakeCategoryTreee.java
 * @Package phoenics.apeak.simple.service
 * @Description
 * @author phoenics@126com
 * @date 2016-1-30 16:01:31
 * @version V1.0
 */
package phoenics.apeak.simple.service;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;

import phoenics.apeak.crawl.Extractor;
import phoenics.apeak.simple.entity.CategoryTree;
import phoenics.apeak.simple.entity.UrlInfo;
import phoenics.apeak.simple.store.CategoryTreeDao;
import phoenics.apeak.simple.store.UrlInfoDao;
import phoenics.apeak.simple.entity.LimitCategory;
// TODO: Auto-generated Javadoc
import phoenics.apeak.simple.store.LimitCategoryDao;

/**
 * The Class TakeCategoryTreee.
 *
 * @Description The Class TakeCategoryTreee.
 */
@Service
public class TakeCategoryTreee implements Extractor{
	
	/** The category mapper. */
	ObjectMapper categoryMapper = new ObjectMapper();
	
	/** The category tree dao. */
	@Autowired
	CategoryTreeDao categoryTreeDao;
	
	/** The url info dao. */
	@Autowired
	UrlInfoDao urlInfoDao;
	@Autowired
	LimitCategoryDao limitCategoryDao;
	/* (non-Javadoc)
	 * @see phoenics.apeak.crawl.Extractor#setParams(java.lang.String)
	 */
	@Override
	public void setParams(String params) {
		// TODO Auto-generated method stub
		
	}

	/* (non-Javadoc)
	 * @see phoenics.apeak.crawl.Extractor#setUrl(java.lang.String)
	 */
	@Override
	public void setUrl(String url) {
		// TODO Auto-generated method stub
		
	}

	/* (non-Javadoc)
	 * @see phoenics.apeak.crawl.Extractor#extract(byte[])
	 */
	@Override
	public void extract(byte[] content) {
		try {
			//categories_url="http://www.shopstyle.com/api/v2/categories?fields=id,name,shortName,parentId,localizedId&pid=shopstyle"
			JsonNode rootNode=categoryMapper.readTree(content);
			ObjectNode metadata = (ObjectNode) rootNode;
			ArrayNode categories=(ArrayNode)metadata.findValue("categories");
			Iterator<JsonNode> iterator = categories.elements();
			//Map<String ,CategoryTree> mc=new HashMap<>();
			List<CategoryTree> mc =new ArrayList<>();
			Map<String ,String> m2=new HashMap<>();
			Map<String, String> limit=getLimitCategories();
			while (iterator.hasNext()) {
				ObjectNode p=(ObjectNode)iterator.next();
				CategoryTree t=new CategoryTree();
				t.setId(p.get("id").asText());
				t.setName(p.get("name").asText());
				t.setParentId(p.get("parentId").asText());
				mc.add(t);
				m2.put(p.get("parentId").asText(), "");
				categoryTreeDao.save(t);
			}
			for(CategoryTree ct:mc){
				if(m2.get(ct.getId())==null && limit.get(ct.getId())!=null){
					UrlInfo urlInfo=new UrlInfo();
					urlInfo.setId(ct.getId());
					urlInfo.setLimit(50);
					urlInfo.setOffset(0);
					urlInfo.setTotal(-1);
					urlInfo.setOver(0);
					urlInfoDao.save(urlInfo);
				}
			}
			mc=null;
			m2=null;
		} catch (JsonProcessingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	private Map<String, String> getLimitCategories(){
		Map<String ,String> m2=new HashMap<>();
		List<LimitCategory> alllimit=limitCategoryDao.findAll();
		alllimit.forEach(c->{
			m2.put(c.getId(), "");
		});
		return m2;
	}
}
